package cn.cxy.demo

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.contentTv
import kotlinx.android.synthetic.main.activity_main2.*

/**
 * 利用rxjava结合Retrofit串行发送两个网络请求
 */
class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        queryData()
    }

    private fun queryData() {
        val networkService = getNetworkService()
        networkService.query()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .map { contentTv.text = it.name }
            .observeOn(Schedulers.io())
            .flatMap { return@flatMap networkService.query2() }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ contentTv2.text = it.name },
                { Log.e("MainActivity2", it.message) }, {})
    }
}